package v1

import (
	"fmt"
	"usplatformserver/internal/model"
	"usplatformserver/internal/service"
	"usplatformserver/pkg/app"
	"usplatformserver/pkg/errcode"

	"github.com/gin-gonic/gin"
)

// @Summer 获取人工判读统计数据
// @Produce json
// @Param token header string true "token"
// @Param organType query int true "检查部位"
// @Param beginTime query string false "起始时间"
// @Param endTime query string false "截止时间"
// @Success 200 {object} model.ArtificialCheckedStc "成功"
// @Failure 400 {object} errcode.Error "请求错误"
// @Failure 500 {object} errcode.Error "内部错误"
// @Router /api/v1/artificial/checked/stat [get]
func GetArtificialCheckedStc(ctx *gin.Context) {
	param := service.AnalysisStatisticsRequest{}

	response := app.NewResponse(ctx)

	valid, errs := app.BindAndValid(ctx, &param)
	if !valid {
		response.ToErrorResponse(errcode.InvalidParams.WithDetails(errs.Errors()...))
		return
	}

	fmt.Print("beginTime: ", param.BeginTime, " endTime: ", param.EndTime)

	svc := service.New(ctx.Request.Context())

	ret, err := svc.GetArtificialStatistics(*param.OrganType, param.BeginTime, param.EndTime)
	if err != nil {
		response.ToErrorResponse(errcode.ErrorArtificialCheckedStat)
		return
	}
	response.ToResponse(ret)
}

// @Summer 获取心脏人工判读统计数据
// @Produce json
// @Param token header string true "token"
// @Param organType query int true "检查部位"
// @Param beginTime query string false "起始时间"
// @Param endTime query string false "截止时间"
// @Success 200 {object} model.HeartAttificialCheckedStc "成功"
// @Failure 400 {object} errcode.Error "请求错误"
// @Failure 500 {object} errcode.Error "内部错误"
// @Router /api/v1/heart/artificial/checked/stat [get]
func GetHeartArtificialStatistics(ctx *gin.Context) {
	param := service.AnalysisStatisticsRequest{}
	response := app.NewResponse(ctx)

	valid, errs := app.BindAndValid(ctx, &param)
	if !valid && param.OrganType != &model.CheckpointHeart {
		response.ToErrorResponse(errcode.InvalidParams.WithDetails(errs.Errors()...))
		return
	}
	fmt.Print("beginTime: ", param.BeginTime, " endTime: ", param.EndTime)
	svc := service.New(ctx.Request.Context())

	ret, err := svc.GetHeartArtificialStatistics(param.BeginTime, param.EndTime)
	if err != nil {
		response.ToErrorResponse(errcode.ErrorHeartArtificialCheckedStat)
		return
	}
	response.ToResponse(ret)
}

// @Summer 获取心脏人工判读统计数据(第二版)
// @Produce json
// @Param token header string true "token"
// @Param organType query int true "检查部位"
// @Param beginTime query string false "起始时间"
// @Param endTime query string false "截止时间"
// @Success 200 {object} model.HeartAttificialCheckedStcSwagger2 "成功"
// @Failure 400 {object} errcode.Error "请求错误"
// @Failure 500 {object} errcode.Error "内部错误"
// @Router /api/v1/heart/artificial/checked/stat2 [get]
func GetHeartArtificialStatistics2(ctx *gin.Context) {
	param := service.AnalysisStatisticsRequest{}
	response := app.NewResponse(ctx)

	valid, errs := app.BindAndValid(ctx, &param)
	if !valid && param.OrganType != &model.CheckpointHeart {
		response.ToErrorResponse(errcode.InvalidParams.WithDetails(errs.Errors()...))
		return
	}
	fmt.Print("beginTime: ", param.BeginTime, " endTime: ", param.EndTime)
	svc := service.New(ctx.Request.Context())

	ret, err := svc.GetHeartArtificialStatistics2(param.BeginTime, param.EndTime)
	if err != nil {
		response.ToErrorResponse(errcode.ErrorHeartArtificialCheckedStat)
		return
	}
	response.ToResponse(ret)
}

// @Summer 获取心脏人工判读统计数据
// @Produce json
// @Param token header string true "token"
// @Param beginTime query string false "起始时间"
// @Param endTime query string false "截止时间"
// @Success 200 {object} model.ArtificialCheckedDetailSwagger "成功"
// @Failure 400 {object} errcode.Error "请求错误"
// @Failure 500 {object} errcode.Error "内部错误"
// @Router /api/v1/artificial/stat/detail [get]
func GetArtificialStatisticsDetail(ctx *gin.Context) {
	param := service.ProductUsageRateRequest{}
	response := app.NewResponse(ctx)

	valid, errs := app.BindAndValid(ctx, &param)
	if !valid {
		response.ToErrorResponse(errcode.InvalidParams.WithDetails(errs.Errors()...))
		return
	}
	fmt.Print("beginTime: ", param.BeginTime, " endTime: ", param.EndTime)
	svc := service.New(ctx.Request.Context())

	ret, err := svc.GetArtificialStatisticsDetail(param.BeginTime, param.EndTime)
	if err != nil {
		response.ToErrorResponse(errcode.ErrorGetArtiStatDetailFail)
		return
	}
	response.ToResponseListWithoutPager(ret)
}
